/*
 * Copyright (c) 2005, the JUNG Project and the Regents of the University of
 * California All rights reserved.
 *
 * This software is open-source under the BSD license; see either "license.txt"
 * or http://jung.sourceforge.net/license.txt for a description.
 *
 *
 * Created on Apr 2, 2005
 */
package com.davetrudes.jung.client.visualization.picking;

import java.util.HashMap;

import com.davetrudes.jung.client.visualization.event.ItemEvent;
import com.davetrudes.jung.client.visualization.event.ItemListener;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.SimpleEventBus;

/**
 * An abstract class to support ItemEvents for PickedState
 *
 * @author Tom Nelson
 */
public abstract class AbstractPickedState<T> implements PickedState<T> {

	protected EventBus eventBus = new SimpleEventBus();
	protected HashMap<ItemListener, HandlerRegistration> handlerMap = new HashMap<ItemListener, HandlerRegistration>();
	
    public void addItemListener(ItemListener l) {
    	HandlerRegistration hr = eventBus.addHandler(ItemEvent.TYPE, l);
    	handlerMap.put(l, hr);
    }

    public void removeItemListener(ItemListener l) {
    	HandlerRegistration hr = handlerMap.get(l);
    	hr.removeHandler();
    	handlerMap.remove(l);
    }

    protected void fireItemStateChanged(ItemEvent e) {
    	eventBus.fireEvent(e);
    }
}